<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增供应商</title>
    <link rel="stylesheet" href="/view/frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/view/frame/static/css/global.css?v=1.0">
    <style>
      body {margin:20px;}
      .layui-form-item--require label::before {
            content: '*';
            position: relative;
            z-index: 10;
            left: -5px;
            font-family: 'simsun', '宋体';
            font-size: 15px;
            color: #f00;
      }
      .form-txt {color:#999; font-size:12px; margin-top:10px; display:block;}
      .layui-form-item .layui-form-label {width:130px;}
      .layui-form-item .layui-input-block {margin-left:160px;}
      /* .layui-form-item {margin-bottom: 0;} */
      .layui-form-item .addressWidth {width: 100px; margin-right: 20px;margin-bottom: 10px;}
      .hide-box {display:none;}
      .hide-btn {text-align:right; margin-bottom:-20px;}
      .hide-btn a {font-size:12px; color:#1E9FFF;}
      .hide-btn a i {margin-left:5px; font-size:12px;}
      /* .layui-btn-small {height: 20px; line-height: 20px; padding: 0px 25px 0; font-size: 12px; float: right} */
      .layui-field-title {margin-bottom: 10px}
      .hold {margin-top: 10px; width:120px; letter-spacing:5px;}
      #linkDistrict .layui-input-inline {width: 160px;}
    </style>
  </head>
  <body>
    <form class="layui-form" autocomplete="off">
      <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>基本信息</legend>
      </fieldset>
      <div class="layui-fluid">
        <div class="layui-row">
          <div class="layui-col-sm6">
            <div class="layui-form-item layui-form-item--require">
              <label class="layui-form-label"><span id="uscId">统一社会信用代码：</span></label>
              <div class="layui-input-block">
                <input type="text" name="usc" lay-verify="usc" class="layui-input" maxlength="18">
              </div>
            </div>
          </div>
          <div class="layui-col-sm6">
            <div class="layui-form-item layui-form-item--require">
              <label class="layui-form-label">供应商名称：</label>
              <div class="layui-input-block">
                <input type="text" name="name" lay-verify="name" class="layui-input" maxlength="50">
              </div>
            </div>
          </div>  
          <div class="layui-col-sm6">
            <div class="layui-form-item layui-form-item--require">
              <label class="layui-form-label">供应商简称：</label>
              <div class="layui-input-block">
                <input type="text" name="nameShort" lay-verify="nameShort" class="layui-input" maxlength="20">
              </div>
            </div>
          </div>
          <div class="layui-col-sm6">
            <div class="layui-form-item layui-form-item--require">
              <label class="layui-form-label">供应商类型：</label>
              <div class="layui-input-block">
                <select name="typeDcode" lay-verify="typeDcode">
                  <option value="" selected="selected">请选择</option>
                </select>
              </div>
            </div>
          </div>
          <div class="layui-col-sm12">
            <div class="layui-form-item layui-form-item--require" id="linkDistrict">
              <label class="layui-form-label">详细地址</label>
              <div class="layui-input-inline">
                <select name="linkProvince" lay-filter="linkProvince">
                  <option value="">请选择</option>
                </select>
              </div>
              <div class="layui-input-inline">
                <select name="linkCity" lay-filter="linkCity">
                  <option value="">请选择</option>
                </select>
              </div>
              <div class="layui-input-inline">
                <select name="linkCounty" lay-filter="linkCounty">
                  <option value="">请选择</option>
                </select>
              </div>
              <div class="layui-input-inline">
                <select name="linkStreet" lay-filter="linkStreet">
                  <option value="">请选择</option>
                </select>
              </div>
              <input type="hidden" name="linkmanAreaId">
              <div class="layui-input-inline" style="width:220px;">
                <input type="text" name="linkmanAddress" lay-verify="linkmanAddress" placeholder="请输入详细地址" class="layui-input" maxlength="120">
              </div>
            </div>
          </div>

        </div>


      <table id="tableListTP" lay-filter="tableSSC"></table>
      <input type="hidden" name="dataSSC">
      <div id="hold" class="layui-form-item" style="text-align:center; margin-top:20px;">
        <button class="layui-btn layui-btn-normal hold" id="vsubmit" lay-submit lay-filter="submit">保存</button>
        <button class="layui-btn layui-btn-primary hold" type="reset">重置</button>
      </div>
      
    </form>
  </body>
  <script charset="utf-8" src="/view/frame/layui/layui.js"></script>
  <script charset="utf-8" src="/view/frame/static/js/jquery.min.js?v=1.0"></script>
  <script charset="utf-8" src="/view/frame/static/js/lang_zh_CN.js?v=1.0"></script>
  <script charset="utf-8" src="/view/frame/static/js/layui.district.js?v=1.0"></script>
  <script charset="utf-8" src="/view/tms/static/js/tms_tab.js?v=1.0"></script>
  <script charset="utf-8" src="/view/tms/static/js/tms_index.js?v=1219"></script>
  
  <script charset="utf-8" src="/view/frame/static/js/errorCodeMap.js"></script>
  <script charset="utf-8" src="/view/frame/static/js/HC.js?v=1.4"></script>
  <script charset="utf-8" src="/view/tms/static/js/validator.js"></script>
  <script charset="utf-8" src="/view/tms/static/js/bizUtil.js"></script>
  <script>

  layui.use(['form', 'layer', 'layedit', 'laydate','table'], function(){
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        tmsTab = layui.tms_tab,
        $ = layui.jquery;

    // $('#userDistrict').district(form);
    // $('#juryDistrict').district(form);

    $('#linkDistrict').district(form);

    //初始化供应商类型
    $('select[name="typeDcode"]').append(getSupplierCustomerList());
    form.render('select');
    
    // uscId
    //自定义验证规则
    form.verify({
      name: function(v){
        if(v.length == 0){
          return '请输入供应商名称';
        }else if(v.length < 2 || v.length > 50){
          return '供应商名称为长度为2到50位字符';
        }else if(!$jsReg.zhCompanyName.test(v)){
          return '供应商名称格式不正确';
        }
      },
      nameShort: function(v){
        if(v.length == 0){
          return '请输入简称';
        }else if(v.length < 2 || v.length > 20){
          return '简称为长度为2到20位字符';
        }else if(!$jsReg.zhCompanyName.test(v)){
          return '简称格式不正确';
        }
      },
      usc: function(v){
        //查找公司信息是否存在（统一信用代码）
        //var $coDetail = getCompanyByUsc($.trim(v));
        if(v.length == 0){
          return '请输入正确的信用代码';
        }else if(!$jsReg.unifyCode.test(v)){
          return '信用代码格式不正确，支持大小写英文和数字，18字符';
        }
        // else if($coDetail == null){
        //   return '公司信息不存在';
        // }
      },
      typeDcode: function(v){
        if(v.length == 0){
          return '请选择供应商类型';
        }
      },
      linkmanAreaId: function(v){
        if(v.length == 0){
          return '请选择城市';
        }
      },
      linkmanAddress: function(v){
        if(v.length == 0){
          return '请输入详细地址';
        }else if(!$jsReg.zhCompanyName.test(v)){
          return '详细地址格式不正确';
        }else if(v.length > 120){
          return '详细地址长度最多120位';
        }
      }
    });


    $('input[name="usc"]').on('blur', function(){
        var $usc = $.trim($(this).val());
        if($usc.length == 18){
          //获取企业详情
          var $companyDetail = getCompanyByUsc($usc);
          if($companyDetail != null){
            $('input[name="name"]').val($companyDetail.coName);
            $('input[name="nameShort"]').val($companyDetail.coNameShort);
          }
        }
    });            

    form.on('submit(submit)', function(data){
        var $index1 = 3 - $('#linkDistrict .layui-input-inline:hidden').length;
            $indexVal1 = $('#linkDistrict .layui-input-inline').eq($index1).find('select').val();
            $('input[name="linkmanAreaId"]').val($indexVal1);

        if($('input[name="linkmanAreaId"]').val().length == 0){
            parent.layer.msg('请选择城市');
            return false;
        }

        var $linkmanAreaId = $('input[name="linkmanAreaId"]').val().length > 0 ? parseInt($.trim($('input[name="linkmanAreaId"]').val())) : '';
            // $juryAreaId = $('input[name="juryAreaId"]').val().length > 0 ? parseInt($.trim($('input[name="juryAreaId"]').val())) : '';
        //数据源       
        var $saveData = {
            linkmanAddress:$.trim(data.field.linkmanAddress),
            linkmanAreaId:$linkmanAreaId,
            name:$.trim(data.field.name),
            nameShort:$.trim(data.field.nameShort),
            typeDcode:$.trim(data.field.typeDcode),
            usc:$.trim(data.field.usc)
        };

        //保存数据，调用接口
        HC.ajax.post({
            url:'/ucenter/crm/supplier/supplier/add.shtml',                  
            data:JSON.stringify($saveData), 
            success:function(d){
                var supplierOption = {
                    // 供应商列表                                                         
                    url: '/ucenter/crm/supplier/supplier/search.shtml',
                    element: $(window.parent.document).find('#supplierList'),
                    keyConvertMap: {
                        id: 'id',
                        nameShort: 'name'  
                    }                        
                };

                HC.ajax.get({
                    url:supplierOption.url,                 
                    success: function(res) {
                        console.log(res)
                        bizUtil.layui.renderSelect($.extend({
                            layuiFormModule: parent.layui.form,
                            // afterRenderCallback: function ($_dropDownList, settings) {},
                            dataList: res
                        }, supplierOption));
                        parent.layer.alert('保存成功！', {
                            yes: function(){           
                            parent.layer.closeAll();
                        }
                    });
                    }
                });                
            },
            bizError: function(res){
                switch(res.code){
                    case "ERROR_PWD_CANNOT_NULL":
                        parent.layer.alert('密码不能为空！');
                    break;
                    case "ERROR_EMPLOYEE_EXIST":
                        parent.layer.alert('身份证已存在！');
                    break;
                    case "ERROR_MOBILE_EXIST":
                        parent.layer.alert('手机号已存在！');
                    break;
                    case "ERROR_USER_EXIST":
                        parent.layer.alert('登录帐号已被使用！');
                    break;
                    default:   
                        parent.layer.alert('保存信息失败，请重新填写！');
                }
            }
        });
        return false;
    });
  });
  </script>
</html>